var accountDao = function() {}
const build_list = require("../../config/build_list.json")
const build_type = require("../../config/build_type.json")
//创建新账号
accountDao.prototype.createAccount = function(otps,cb) {
	if(!otps.unionid){
		cb(false,"unionid error "+otps.unionid)
		return
	}
	var userInfo = {
		unionid : otps.unionid,
		head : otps.head || "",
		name : otps.name || "",
		limit : 0,
		dayStr : (new Date()).toDateString()
	}
	var self = this
	self.redisDao.db.incrby("acc:lastid",Math.floor(Math.random() * 100) + 1,function(err,data) {
		if(!err && data){
			userInfo.uid = data
			self.redisDao.db.hmset("acc:user:"+userInfo.uid,userInfo)
			//建立映射
			self.redisDao.db.hset("acc:uidMap:unionid",userInfo.unionid,userInfo.uid)
			self.initPlayerInfo(userInfo.uid)
			cb(true,userInfo)
		}else{
			cb(false,"createAccount error")
		}
	})
}
//获取账号信息
accountDao.prototype.getAccountInfo = function(otps,cb) {
	var unionid = otps.unionid
	var self = this
	self.redisDao.db.hget("acc:uidMap:unionid",unionid,function(err,uid) {
		if(err || !uid){
			cb(false)
		}else{
			self.redisDao.db.hgetall("acc:user:"+uid,function(err,data) {
				cb(true,data)
			})
		}
	})
}
//初始化玩家信息
accountDao.prototype.initPlayerInfo = function(uid) {
	var playerInfo = {
		"gold" : 10000,
		"assets" : 0,
		"crossword_num" : 0,
		"ship_time" : 0
	}
	for(var i = 1;i <= 9;i++){
		playerInfo["grid_build_"+i] = 0
		playerInfo["grid_time_"+i] = 0
		playerInfo["grid_award_"+i] = 0
	}
	playerInfo["grid_build_1"] = 1
	playerInfo["grid_build_2"] = 1
	playerInfo["grid_build_3"] = 1
	playerInfo["gridNumber"] = 3
	for(var i in build_list){
		playerInfo["build_"+i] = 0
	}
	for(var i = 1;i <= 3;i++){
		playerInfo["ship_build_"+i] = 0
		playerInfo["ship_count_"+i] = 0
	}
	this.redisDao.db.hmset("playerInfo:user:"+uid,playerInfo)
}
//获取玩家信息
accountDao.prototype.getPlayerInfo = function(uid,cb) {
	this.redisDao.db.hgetall("playerInfo:user:"+uid,function(err,data) {
		for(var i in data){
			data[i] = Number(data[i])
		}
		cb(true,data)
	})
}
//获取账号属性
accountDao.prototype.getAccountData = function(otps,cb) {
	var uid = otps.uid
	var name = otps.name
	var self = this
	self.redisDao.db.hget("acc:user:"+uid,name,function(err,data) {
		if(err || !data){
			cb(false)
		}else{
			cb(true,data)
		}
	})
}
module.exports = {
	id : "accountDao",
	func : accountDao,
	props : [{
		name : "redisDao",
		ref : "redisDao"
	}]
}